package com.example.project;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.loader.content.AsyncTaskLoader;

import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


import com.example.project.entity.AddDaily;
import com.example.project.entity.Data;
import com.example.project.entity.Root;
import com.example.project.service.ConDataManager;

import java.util.Date;

public class MainActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.timer);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd");
        String time = simpleDateFormat.format(date);
        textView.setText(time);
        Button button = findViewById(R.id.naniu);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);

            }
        });
        ReadTask readTask = new ReadTask();
        readTask.execute();


    }

    class ReadTask extends AsyncTask<Void, Void, Root> {


        @Override
        protected Root doInBackground(Void... voids) {

                ConDataManager conDataManager = new ConDataManager();
                try {
                    return conDataManager.getData();

                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }

    }

        @Override
        protected void onPostExecute(Root root) {
            ConDataManager.root = root;
            refreshUI(root);
        }

        }

    private void refreshUI(Root jsonRoot) {

        Data data = jsonRoot.getData();
        AddDaily addDaily=data.getAddDaily();

       TextView heconNum= findViewById(R.id.heconNum);
       TextView hecon=findViewById(R.id.hecon);
       heconNum.setText(data.getHeconNum()+"");
       hecon.setText(addDaily.getAddhecon());

      TextView jwsrNum= findViewById(R.id.jwsrNum);
       TextView jwsr= findViewById(R.id.jwsr);
       jwsrNum.setText(data.getJwsrNum()+"");
       jwsr.setText(addDaily.getAddjwsr()+"");

      TextView asymptomNUm=findViewById(R.id.asymptomNUm);
      TextView asymptom= findViewById(R.id.asymptom);
      asymptomNUm.setText(data.getAsymptomNum()+"");
      asymptom.setText(addDaily.getAddasymptom()+"");

      TextView ExistingNum   = findViewById(R.id.ExistingNum);
      TextView Existing = findViewById(R.id.Existing);
      ExistingNum .setText(data.getEconNum()+"");
      Existing.setText(addDaily.getAddecon()+"");



      TextView GntotalNum = findViewById(R.id.GntotalNum);
      TextView Gntotal = findViewById(R.id.Gntotal);
      GntotalNum.setText(data.getGntotal()+"");
      Gntotal.setText(addDaily.getAddcon()+"");

        TextView DeathtotalNum = findViewById(R.id.DeathtotalNum);
        TextView Deathtotal= findViewById(R.id.Deathtotal);
        DeathtotalNum.setText(data.getDeathtotal()+"");
        Deathtotal.setText(addDaily.getAdddeath()+"");


        TextView cureTotalNum = findViewById(R.id.cureTotalNum);
        TextView cureTotal= findViewById(R.id.cureTotal);
        cureTotalNum.setText(data.getCuretotal()+"");
        cureTotal.setText(addDaily.getAddcure()+"");

        TextView SustotalNum = findViewById(R.id.SustotalNum);
        TextView Sustotal= findViewById(R.id.Sustotal);

       SustotalNum.setText(data.getSustotal()+"");
       Sustotal.setText(addDaily.getAddsus()+"");

    }

}
